<?php
require_once '../dao/WatcherDao.php';

$action = "";
if (isset($_GET['action'])) {
	$action = $_GET['action'];
}

switch ($action) {
	case 'countWatcherFrom' :
		countWatcherFrom();
		break;
	case 'countWatcherTo' :
		countWatcherTo();
		break;
	case 'checkFollow' :
		checkFollow();
		break;
	case 'followUnfollow' :
		followUnfollow();
		break;
	case 'getWatchers' :
		getWatchers();
		break;
	case 'getFollowers' :
		getFollowers();
		break;
	default :
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no action found";
		print json_encode($jsonResult);
		break;
}

/*
 * get the number of the watcher that the masker follows
 */
function countWatcherFrom() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if id is set
	if (!isset($_POST['id'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id found";
	} else {
		$id = $_POST['id'];
		$record = $watcherDao -> getCountWatcherFrom($id);
		$jsonResult['result'] = $record;
	}

	print json_encode($jsonResult);
}

/*
 * get the number of the watcher that follows the masker
 */
function countWatcherTo() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if id is set
	if (!isset($_POST['id'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id found";
	} else {
		$id = $_POST['id'];
		$record = $watcherDao -> getCountWatcherTo($id);
		$jsonResult['result'] = $record;
	}

	print json_encode($jsonResult);
}

/*
 * check if you have the follow watcher
 * return 0 not follow, 1 follow
 */
function checkFollow() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if idMaskerLogged and idMaskerProfile are set
	if (!isset($_POST['idMaskerLogged'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id maskerLogged found";
	} else if (!isset($_POST['idMaskerProfile'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id maskerProfile found";
	} else {
		$idMaskerLogged = $_POST['idMaskerLogged'];
		$idMaskerProfile = $_POST['idMaskerProfile'];
		$record = $watcherDao -> checkFollow($idMaskerLogged, $idMaskerProfile);
		
		$jsonResult['result'] = $record;
	}

	print json_encode($jsonResult);
}

/*
 * onclick button follow / unfollow a masker
 */
function followUnfollow() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if idMaskerLogged and idMaskerProfile are set
	if (!isset($_POST['idMaskerLogged'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id maskerLogged found";
	} else if (!isset($_POST['idMaskerProfile'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id maskerProfile found";
	} else if (!isset($_POST['toFollow'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no toFollow action found";
	} else {
		$idMaskerLogged = $_POST['idMaskerLogged'];
		$idMaskerProfile = $_POST['idMaskerProfile'];
		$toFollow = $_POST['toFollow'];
		
		$record = $watcherDao -> followUnfollow($idMaskerLogged, $idMaskerProfile, $toFollow);
		$jsonResult['result'] = $record;
	}

	print json_encode($jsonResult);	
}


/*
 * get the watchers that follows the masker
 */
function getFollowers() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if name is set
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id found";
	} else {
		$name = $_POST['name'];
		$records = $watcherDao -> getFollowers($name);
		$jsonResult['result'] = $records;
	}

	print json_encode($jsonResult);
}


/*
 * get the watchers that the masker follow
 */
function getWatchers() {
	$watcherDao = new WatcherDao();
	$jsonResult = array();

	// check if name is set
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id found";
	} else {
		$name = $_POST['name'];
		$records = $watcherDao -> getWatchers($name);
		$jsonResult['result'] = $records;
	}

	print json_encode($jsonResult);
}

?>